import os
import tushare as ts
import datetime
import re
import multiprocessing as mp
import threading as td
import time
import requests
import stockAlgorithm as sa
import shutil

# 清空showCsv
shutil.rmtree('showCsv')
os.mkdir('showCsv')
# 初始时间和结束时间
now = datetime.datetime.now()
timeStamp = now.strftime('%Y%m%d')
endTime = now.strftime('%Y%m%d')
startTime = (now + datetime.timedelta(days=-20)).strftime('%Y%m%d')
if os.path.exists('result.txt'):
    os.remove('result.txt')
# 第一步：获取所有上市股票
def stepOne():
    # 判断股票列表是否存在,不存在则生成
    if not os.path.exists('csv/Stocks%s.csv'%timeStamp):
        # 删除之前的文件
        filelist = os.listdir('csv/')
        for file in filelist:
            if re.match('Stocks[0-9]{8}\.csv', file):
                os.remove('csv/%s' % file)
                break
        pro = ts.pro_api("a336c08f0856dd1484c70dd3a5d6780e77bab90031555891d439824e")
        # tushare的接口，获取所有上市的股票
        data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
        data.to_csv('csv/Stocks%s.csv' % timeStamp)
        # 跳转到第二步
    stepTwo()
# 第二步：分别获取个股数据，并用算法筛选
def stepTwo():
    # 创建一个多线程队列
    codeQueue = mp.Queue()
    # 获取所有股票代码
    with open('csv/Stocks%s.csv' % timeStamp, "r", encoding='utf-8') as fd:
        lines = fd.readlines()
        for line in lines:
            line = line = line.strip().split(",")
            code = line[2]
            codeQueue.put(code)
    # 创建4个线程
    processList = []
    for _ in range(4):
        process = mp.Process(target=openThread, args=(codeQueue,))
        process.start()
        processList.append(process)
    while True:
        if codeQueue.qsize() <= 0:
            for item in processList:
                item.terminate()
                item.join()
            break
    return True
# 开启多线程
def openThread(codeQueue):
    ThreadList = []
    for _ in range(8):
        thread = td.Thread(target=excuteThread, args=(codeQueue,))
        thread.start()
    while True:
        if codeQueue.qsize() <= 0:
            for thread in ThreadList:
                thread.join()
            break
        time.sleep(1)
# 获取个股数据并执行算法
def excuteThread(codeQueue):
    while codeQueue.qsize()>0:
        code = codeQueue.get()
        type = '0' if re.match('6[0-9]{5}', code) else '1'
        url = 'http://quotes.money.163.com/service/chddata.html?code=' + type + code + '&start=' + startTime + '&end=' + endTime
        response = requests.get(url, timeout=500)
        print(response.text)
        res = response.text
        arr = res.split('\r\n')
        response.close()
        highlist = []
        lowlist = []
        prilist = []
        openlist = []
        vollist = []
        rangelist = []
        stock = {
            'code': '',
            'name': ''
        }
        for i in range(1, len(arr)):
            # 可能存在某一天没有数据
            if arr[i] == "":
                continue
            # 日期0,股票代码1,名称2,收盘价3,最高价4,最低价5,开盘价6,前收盘7,涨跌额8,涨跌幅9,换手率10,成交量11,成交金额12,总市值13,流通市值14,成交笔数15
            lis = arr[i].split(',')
            if lis[9] == "None":
                continue
            stock['code'] = re.sub('\'', '', lis[1])
            stock['name'] = lis[2]
            highlist.append(float(lis[4]))
            prilist.append(float(lis[3]))
            lowlist.append(float(lis[5]))
            openlist.append(float(lis[6]))
            vollist.append(float(lis[11]))
            rangelist.append(float(lis[9]))
        if len(prilist) >= 5:
            algorithm = sa.Algorithm(highlist, lowlist, openlist, prilist, vollist, rangelist, stock)
            alg1 = algorithm.sharesSize1()
            alg2 = algorithm.sharesSize2()
            alg3 = algorithm.sharesSize3()
            alg4 = algorithm.sharesSize4()
            if alg1:
                stepThree(stock['code'],stock['name'],1)
            if alg2:
                stepThree(stock['code'],stock['name'],2)
            if alg3:
                stepThree(stock['code'],stock['name'],3)
            if alg4:
                stepThree(stock['code'],stock['name'],4)
        time.sleep(.2)
def stepThree(code,name,type):
    fp = open('result.txt','a',encoding='utf-8')
    fp.write('%s,%s,%s\n' % (code, name, type))
    fp.close()
    pro = ts.pro_api("a336c08f0856dd1484c70dd3a5d6780e77bab90031555891d439824e")
    endTime = datetime.datetime.now().strftime('%Y%m%d')
    startTime = (datetime.datetime.now() + datetime.timedelta(days=-31)).strftime('%Y%m%d')
    type = '.SH' if re.match('6[0-9]{5}', code) else '.SZ'
    df = pro.daily(ts_code='%s%s' % (code, type), start_date=startTime, end_date=endTime)
    df.to_csv('showCsv/%s.csv' % code)

if __name__ == '__main__':
    stepOne()